define([
    "dojo/_base/declare",
    "dijit/_Widget",
    "dijit/_Templated",
    "dojo/text!./SettingForm/template/form.html",
    "dojo/_base/kernel",
    "dijit/form/TextBox",
    "dijit/form/Select",
    "dijit/form/Button",
    "dijit/form/SimpleTextarea"],
    function(declare, _Widget,_Templated,templateHtml,dojo){
        return declare("widget.SettingForm",[_Widget,_Templated], {
            widgetsInTemplate: true,
            baseClass: "setting-form",
            templateString: templateHtml,
            postCreate: function(){
                this.inherited(arguments);

                dojo.connect(this.saveButton,"onClick",dojo.hitch(this,function(){
                    this.save();
                }));
                this.loadSettings();
            },
            
            save: function(){
                var p = {};
                var WeightPrice = [
                    {
                    "ID":3,//Product Type ID
                    "MaxPrice":dijit.byId("setting_t_max_price").get("value"),
                    "MinPrice":dijit.byId("setting_t_min_price").get("value"),
                    "MaxWeight":dijit.byId("setting_t_max_weight").get("value"),
                    "MinWeight":dijit.byId("setting_t_min_weight").get("value")
                    },
                    {
                    "ID":4,//Product Type ID
                    "MaxPrice":dijit.byId("setting_c_max_price").get("value"),
                    "MinPrice":dijit.byId("setting_c_min_price").get("value"),
                    "MaxWeight":dijit.byId("setting_c_max_weight").get("value"),
                    "MinWeight":dijit.byId("setting_c_min_weight").get("value")
                    },
                    {
                    "ID":2,//Product Type ID
                    "MaxPrice":dijit.byId("setting_c_max_price").get("value"),
                    "MinPrice":dijit.byId("setting_c_min_price").get("value"),
                    "MaxWeight":dijit.byId("setting_c_max_weight").get("value"),
                    "MinWeight":dijit.byId("setting_c_min_weight").get("value")
                    }
                ];
                p["WeightPrice"] = WeightPrice;
                var RankRule = [];
                for (var i=1;i<=32;i++){
                    var subcatRuleValue=-1;
                    if(i>=1 && i<=2){
                        subcatRuleValue = dijit.byId("setting_subcatrule_1").get("value");
                    }else if(i>=3 && i<=9){
                        subcatRuleValue = dijit.byId("setting_subcatrule_3").get("value");
                    }else if(i>=10 && i<=12){
                        subcatRuleValue = dijit.byId("setting_subcatrule_10").get("value");
                    }else if (i==13){
                        subcatRuleValue = dijit.byId("setting_subcatrule_13").get("value");
                    }else if(i>=14 && i<=21){
                        subcatRuleValue = dijit.byId("setting_subcatrule_14").get("value");
                    }else if(i>=22 && i<=29){
                        subcatRuleValue = dijit.byId("setting_subcatrule_22").get("value");
                    }else if (i==30){
                        subcatRuleValue = dijit.byId("setting_subcatrule_30").get("value");
                    }else if (i==31){
                        subcatRuleValue = dijit.byId("setting_subcatrule_31").get("value");
                    }else if (i==32){
                        subcatRuleValue = dijit.byId("setting_subcatrule_32").get("value");
                    }
                    var object = {"ID":i,"Rule":dijit.byId("setting_rule_" + i).get("value"),"SubcatRule":subcatRuleValue};
                    RankRule.push(object);
                }
                p["RankRule"] = RankRule;
                
                var xhrArgs = {
                    url:"rank/saveranksetting",
                    handleAs: "json",
                    content:{"rankset":dojo.toJson(p)}
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    this.loadSettings();
                    alert(result.message);
                }));
            },
            
            loadSettings: function(){
                var xhrArgs = {
                    url:"rank/getranksetting",
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(function(result){
                   var  rankingRule = result.RankingRule;
                   for (var i=0;i<rankingRule.length;i++){
                       var id = rankingRule[i].ID;
                       var rule = rankingRule[i].Rule;
                       dijit.byId("setting_rule_" + id).set("value",rule);
                       
                       var subcat = dijit.byId("setting_subcatrule_" + id);
                       if (subcat){
                           subcat.set("value",rankingRule[i].SubcatRule);
                       }
                   }
                   
                   var  weightPrice = result.WeightPrice;
                   dijit.byId("setting_c_max_price").set("value",weightPrice["Computer Trolley Case"].MaxPrice);
                   dijit.byId("setting_c_min_price").set("value",weightPrice["Computer Trolley Case"].MinPrice);
                   dijit.byId("setting_c_max_weight").set("value",weightPrice["Computer Trolley Case"].MaxWeight);
                   dijit.byId("setting_c_min_weight").set("value",weightPrice["Computer Trolley Case"].MinWeight);
                   
                   dijit.byId("setting_t_max_price").set("value",weightPrice["Trolley Case"].MaxPrice);
                   dijit.byId("setting_t_min_price").set("value",weightPrice["Trolley Case"].MinPrice);
                   dijit.byId("setting_t_max_weight").set("value",weightPrice["Trolley Case"].MaxWeight);
                   dijit.byId("setting_t_min_weight").set("value",weightPrice["Trolley Case"].MinWeight);
                });
            }
            
        });
    });